package com.evil.log.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import com.evil.log.api.RemoteRequestLogService;
import com.evil.log.mapper.RequestLogMapper;
import com.evil.log.pojo.dto.RequestLogDTO;
import com.evil.log.pojo.entiy.RequestLog;
import com.evil.log.service.RequestLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 请求日志表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
public class RequestLogServiceImpl extends ServiceImpl<RequestLogMapper, RequestLog> implements RequestLogService, RemoteRequestLogService {

    @Override
    public int batchInsert(List<RequestLog> list) {
        return baseMapper.batchInsert(list);
    }

    /**
     * 插入请求日志
     *
     * @param requestLog requestLog
     */
    @Override
    public RequestLogDTO insertRequestLog(RequestLogDTO requestLog) {
        RequestLog rq = BeanUtil.copyProperties(requestLog, RequestLog.class);
        if (this.baseMapper.insert(rq) != 1) {
            log.error("insertRequestLog | error");
            throw new BusinessException(RCodeEnum.ERROR_DB, "保存请求日志失败");
        }
        requestLog.setRequestLogId(rq.getRequestLogId());
        log.error("insertRequestLog | success | requestLogId : {}", rq.getRequestLogId());
        return requestLog;
    }
}
